package com.qst.action.exam;

import com.qst.*;
import com.qst.entity.Exam;
import com.qst.entity.TestPersonnel;
import com.qst.service.AssessmentService;
import com.qst.service.ExamService;
import com.qst.service.ScheduleService;
import com.qst.service.impl.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebServlet("/exam/begin.action")
public class BeginServlet extends BaseServlet {

    private AssessmentService assessmentService = ServiceFactory
            .getService(AssessmentService.class);
    private ExamService examService = ServiceFactory
            .getService(ExamService.class);
    private ScheduleService scheduleService = ServiceFactory
            .getService(ScheduleService.class);

    @Override
    protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TestPersonnel testPersonnel = (TestPersonnel) req.getSession().getAttribute(Constant.
                CURRENT_TESTPERSONNEL);
        int scheduleId = RequestUtil.getInt(req, "id");
        try {
            // 生成某个考生的试题：包含考生信息和试题集合
            Exam exam = examService.begin(testPersonnel, scheduleId);
            req.getSession().setAttribute(Constant.CURRENT_EXAM, exam);
            WebUtil.redirect(req, resp, "/exam/exam.action");
        } catch (ExamException ex) {
            ex.printStackTrace();
            addError(req, ex.getMessage());
            WebUtil.redirect(req, resp, "/exam/list.action");
        }
    }
}
